package cn.icare.cms.controller.restful;

import cn.icare.cms.MemberService;
import cn.icare.cms.aop.UserLog;
import cn.icare.cms.model.enums.CommonStatusEnum;
import cn.icare.cms.model.ro.AddMemberRO;
import cn.icare.cms.model.ro.EditMemberRO;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import cn.icare.cms.model.vo.rest.UserVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/member")
@Api(tags = "REST用户")
public class MemberController extends BaseController {
    @Resource
    private MemberService memberService;

    private final String FALL_STRING = "请求失败,服务繁忙!";


    /**
     * URL:http://{ip}:{port}/rest/member
     * method:PUT
     *
     * @param ro
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateOneFall")
    @ApiOperation(value = "更新单个用户", notes = "t_member表更新单个用户")
    public ResponseEntity<RespMsgVO> updateOne(@RequestBody EditMemberRO ro, HttpServletRequest request) {

        //用户防止重复检查
        boolean isExists= this.memberService.isExists(ro.getId(),ro.getPhone(),ro.getUsername());
        if(isExists)
        {
            return super.getRespFail("用户手机号码或用户名已经存在，不能重复添加");
        }
        if (this.memberService.updateOne(ro)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/member/{id}
     * method:DELETE
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @HystrixCommand(fallbackMethod = "deleteOneFall")
    @ApiOperation(value = "删除单个用户", notes = "t_member表删除符合主键的单个用户")
    public ResponseEntity<RespMsgVO> deleteOne(@PathVariable("id") Long id) {
        if (this.memberService.deleteOne(id)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/member?ids={ids}
     * method:DELETE
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @HystrixCommand(fallbackMethod = "deleteBatchFall")
    @ApiOperation(value = "删除多个用户", notes = "t_member表删除符合主键的多个用户")
    public ResponseEntity<RespMsgVO> deleteBatch(@RequestParam("ids") List<Long> ids) {
        if (this.memberService.deleteBatch(ids)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/member/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个用户", notes = "t_member表返回符合主键的单个用户")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Long id) {
        return super.getRespOk(this.memberService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/member?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "查询多个用户", notes = "user表返回符合条件的多个用户")
    public ResponseEntity<RespMsgVO> getList(
            @RequestParam(name = "type", required = false) Integer type,
                                             @RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "pageNum", required = false, defaultValue = "${cms.defaultValue.pageNum}") Integer pageNum,
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "${cms.defaultValue.pageSize}") Integer pageSize

                                             ) {
        return super.getRespOk(this.memberService.getList(type,name, pageNum, pageSize));
    }


    @PostMapping("/updatestatus")
    @UserLog(desc = "修改用户状态")
    @ApiOperation(value = "修改用户状态", notes = "修改用户状态")
    public ResponseEntity<RespMsgVO> updateStatus(@RequestParam(name = "id") Long id,
                                               @RequestParam(name = "status") Integer status
                                               ) {
        if (this.memberService.updateStatus(id,status)) {
            return super.getRespOk(id);
        } else {
            return super.getRespFail("修改用户状态失败");
        }
    }





    //#region fallback methods
    private ResponseEntity<RespMsgVO> createOneFall(AddMemberRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> updateOneFall(EditMemberRO ro,HttpServletRequest request) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> deleteBatchFall(List<Long> ids) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Long id) {
        return super.getRespFail(this.FALL_STRING);
    }

    //#endregion

}
